<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Weixin extends CI_Controller {
	public function __construct() {
		parent::__construct ();
	}
	public function index() {
		$orderid = $this->uri->segment ( 4 );
		$this->load->model ( 'order_model' );
		$data ['torder'] = $this->order_model->load ( $orderid );
		$userid=$this->session->userdata('id');
		$data['num']=$this->order_model->find_orders_num ( $userid );
		$this->load->view ( 'weixin/index',$data);
	}
	public function pay() {
		$orderid = $this->uri->segment ( 4 );
		$this->load->model ( 'order_model' );
		$data ['torder'] = $this->order_model->load ( $orderid );
		$this->load->view ( 'weixin/index',$data);
	}
	public function call_back_url() {
		$orderid = $this->uri->segment ( 4 );
		$this->load->model ( 'order_model' );
		$order=$this->order_model->load($orderid);
		if($order['state']==5){//货到付款未结算
				$neworder= array (
				'state' => 4,
				'payment_time'=>date ( 'Y-m-d H:i:s', time () )
				);
		}else if($order['state']==0){
				$neworder= array (
				'state' => 1,
				'payment_mode'=> 2,
				'payment_time'=>date ( 'Y-m-d H:i:s', time () )
				);
		}
		if($this->order_model->update($orderid,$neworder)){
			$this->load->model ( 'user_model' );
			$user = $this->user_model->load ( $order['userid'] );
			$num=$this->order_model->find_by_ordertime( $order['userid'], date ( "Y-m-d") );//当天下单次数
			if($num==1){
				$this->sendmessagetouser($order,$user);
			}
			redirect('order/myorder');
		}
	}
	public function notify(){
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
		$data=json_decode(json_encode(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
		if($data['result_code']=='SUCCESS'){
			$this->load->model ( 'order_model' );
			$order=$this->order_model->find_by_orderno($data['out_trade_no']);
			if($order['state']==0){
				$neworder= array (
				'state' => 1,
				'payment_mode'=> 2,
				'payment_time'=>date ( 'Y-m-d H:i:s', time () )
				);
				$this->order_model->update($order['id'],$neworder);//不能在这里发短信，有可能会无限请求
			}elseif($order['state']==5){//货到付款未结算
				$neworder= array (
				'state' => 4,
				'payment_time'=>date ( 'Y-m-d H:i:s', time () )
				);
				$this->order_model->update($order['id'],$neworder);//不能在这里发短信，有可能会无限请求
			}
		}
		if (isset($_GET)){
   			 echo "success";
		}	
		//die();
		//$this -> load -> view('weixin/notify');
	}
	/*
	 * 将订单信息通过短信发送给用户
	 */
	private function sendmessagetouser($order,$user){
		$date=date ( "n月j日",strtotime($order ['receive_time'] ));
		if(!empty($user['late_time'])){
			$date=$date.','.$user['late_time'];
		}else{
			$date=$date.','.'-';
		}
		$phone=$user['cellphone'];
		
		$sid="9f014d8f371d9a94348092594b971d3f";
		$appid="55953fa1a9014153b89e194bee63770a";
		$key="d4435599239290ed5e40df062cc85e37";
		$time=date("YmdHis").'111';
		$sign=md5($sid.$time.$key);
		$templateid="11021";
		
		$url="http://www.ucpaas.com/maap/sms/code?sid=".$sid."&appId=".$appid."&time=".$time."&sign=".$sign."&to=".$phone."&templateId=".$templateid."&param=".$date;
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch, CURLOPT_HEADER, 0 );
		$output = curl_exec ( $ch );
		curl_close ( $ch );
		$output = json_decode($output);
		if($output->resp->respCode=="000000"){//短信发送成功
			$message = array (
					'phone' => $phone,
					'state' => 1,
					'smsid' => $output->resp->templateSMS->smsId,
					'createdate' => $output->resp->templateSMS->createDate
			);
			$this->load->model ( 'message_model' );
			$this->message_model->create($message);//更新短信表
		}
	}
}
